package site.zqliang.sc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class ClientApp {

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/")
    public String webapp() {
        return "hell";
    }

    @GetMapping("/hello")
    public String hello() {
        return "hell123";
    }

    @GetMapping("/greeting")
    public String greet() {
        List<ServiceInstance> instances = client.getInstances("client2");
        System.out.println(instances.size());
        ServiceInstance selectedInstance = instances
                .get(new Random().nextInt(instances.size()));
        return "Hello World: " + selectedInstance.getServiceId() + ":" + selectedInstance
                .getHost() + ":" + selectedInstance.getPort();

    }

    public static void main(String[] args) {
        SpringApplication.run(ClientApp.class, args);
    }

}
